<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript">

// name space for this page
var p00_cust = {
	mid: "${menuId}",
	focusId: "p00_cust_memberId"
};

/// do query
p00_cust.doQuery = function(form) {
    var values = form.getValues();
    var checkKeys = ["cmobile", "carId", "memberId", "cname"];
    var allEmpty = true;
    for (var i = 0; i < checkKeys.length; i++) {
        if (!crm.utils.isEmpty(values[checkKeys[i]])) {
            allEmpty = false;
            break;
        }
    }

    if (allEmpty) {
        Ext.Msg.alert("信息提示", "请至少输入一个查询条件。", function() {
        	Ext.getCmp(p00_cust.focusId).focus();
        });
    } else {
    	p00_cust.store.loadRecords([]);
        p00_cust.store.loadPage(1, {
            params: values
        });
    }
};

// condition panel
p00_cust.condPanel = Ext.create("Ext.form.Panel", {
	region: "north",
    buttonAlign: "left",
    frame: true,
    bodyPadding: 5,
    fieldDefaults: {
    	labelAlign: "left",
        labelWidth: 100,
        width: 240,
        labelStyle: "padding-left: 15px"
    },
    items: [{
        xtype: "fieldset",
    	title: "检索条件",
    	layout: "anchor",
        defaults: {
            anchor: "100%",
            padding: "2 0",
        },
        margin: "0",
    	items: [{
    	    xtype: "container",
            layout: "hbox",
            defaultType: "textfield",
            items: [{
            	id: "p00_cust_memberId",
                fieldLabel: "会员卡号",
                regex: /^[\d\w]{1,15}$/,
                regexText: "会员卡号格式不正确！",
                name: "memberId",
                enableKeyEvents: true,
                listeners: {
                    keyup: {
                        fn: function(text, evt, opts) {
                            if (evt.getKey() == Ext.EventObject.ENTER) {
                                p00_cust.doQuery(p00_cust.condPanel.getForm());
                            }
                        }
                    }
                }
            }, {
                fieldLabel: "手机号码",
                regex: /^1\d{1,10}$/,
                regexText: "手机号码格式不正确！",
                name: "cmobile",
                enableKeyEvents: true,
                listeners: {
                    keyup: {
                        fn: function(text, evt, opts) {
                            if (evt.getKey() == Ext.EventObject.ENTER) {
                            	p00_cust.doQuery(p00_cust.condPanel.getForm());
                            }
                        }
                    }
                }
            }]
        }, {
            xtype: "container",
            layout: "hbox",
            defaultType: "textfield",
            items: [{
                fieldLabel: "车牌号码",
                name: "carId"
            }, {
            	fieldLabel: "姓名",
                name: "cname"
            }/*,{
                xtype: "datefield",
                fieldLabel: "最近消费From",
                format: "Y-m-d",
                name: "lcwDateFrom"
            }, {
                xtype: "datefield",
                fieldLabel: "最近消费To",
                format: "Y-m-d",
                name: "lcwDateTo"
            }*/]
        }, {
        	xtype: "container",
        	html: "<p style='color:#ed9200;padding:5px 15px'>请至少输入一个查询条件。</p>"
        }]
    }],
    buttons: [{
        text: "查询",
        handler: function() {
        	// submit form
        	p00_cust.doQuery(p00_cust.condPanel.getForm());
        }
    }, {
        text: "清空条件",
        handler: function() {
            p00_cust.condPanel.getForm().reset();
            Ext.getCmp(p00_cust.focusId).focus();
        }
    }]
});


// Model -> CustInfo
Ext.define("p00_cust_CustInfo", {
    extend: "Ext.data.Model",
    fields: [
        {name:"cust_id", type:"string"},
        {name:"name", type:"string"},
        {name:"mobile", type:"string"},
        {name:"member_id", type:"string"},
        {name:"idcard", type:"string"},
        {name:"gender", type:"string"},
        {name:"gender_disp_nm", type:"string"},
        {name:"phone1", type:"string"},
        {name:"phone2", type:"string"},
        {name:"phone3", type:"string"},
        {name:"qq", type:"string"},
        {name:"msn", type:"string"},
        {name:"email", type:"string"},
        {name:"addr", type:"string"},
        {name:"province", type:"string"},
        {name:"city", type:"string"},
        {name:"county", type:"string"},
        {name:"industry", type:"string"},
        {name:"birth_year", type:"string"},
        {name:"svc_locate_id", type:"string"},
        {name:"svc_locate_disp_nm", type:"string"},
        {name:"park_space", type:"string"},
        {name:"car_id", type:"string"},
        {name:"car_brand", type:"string"},
        {name:"car_brand_disp_nm", type:"string"},
        {name:"car_type", type:"string"},
        {name:"car_color", type:"string"},
        {name:"car_color_disp_nm", type:"string"},
        {name:"car_size_type", type:"string"},
        {name:"car_size_type_disp_nm", type:"string"},
        {name:"lcw_date", type:"string"},
        {name:"lcw_addr", type:"string"},
        {name:"reg_date", type:"string"}
    ]
});

// store
p00_cust.store = Ext.create("Ext.data.Store", {
    model: p00_cust_CustInfo,
    pageSize: 30,
    proxy: {
        type: "ajax",
        url: "cust/query.shtm",
        actionMethods: {
            read: "POST"
        },
        reader: {
            root: "custInfos",
            totalProperty: "totalCount"
        },
        simpleSortMode: true,
        listeners: {
            exception: function(proxy, resp, op, opts) {
                var obj = Ext.JSON.decode(resp.responseText);
                Ext.Msg.alert("信息提示", obj.errorMsgs);
            }
        }
    }
});

// grid panel
p00_cust.gridPanel = Ext.create("Ext.grid.Panel", {
	region: "center",
	frame: true,
	margin:  0,
	autoScroll: true,
    store: p00_cust.store,
    columns: [
        {
        	text: "操作",
        	menuDisabled: true,
            sortable: false,
            xtype: "actioncolumn",
            width: 80,
            align: "center",
            items: [{
            	icon: "images/icons/edit.png",
                tooltip: "编辑",
                handler: function(grid, rowIndex, colIndex) {
                    var rec = p00_cust.store.getAt(rowIndex);
                    crm.utils.createTopTab({
                    	id: p00_cust.mid + "_edit",
                    	name: "编辑客户信息(" + rec.get("cust_id") + ")",
                    	url: "show_cust.shtm?module=modify",
                        qparams: {
                        	custId: rec.get("cust_id")
                        }
                    });
                }
            }]
        },
        { text: "客户编号",  dataIndex: "cust_id" ,width: 120},
        { text: "姓名", dataIndex: "name"},
        { text: "手机号码", dataIndex: "mobile"},
        { text: "会员卡号", dataIndex: "member_id"},
        { text: "性别", dataIndex: "gender_disp_nm"},
        /*{ text: "电话号码一", dataIndex: "phone1"},
        { text: "电话号码二", dataIndex: "phone2"},*/
        { text: "车牌号", dataIndex: "car_id"},
        { text: "汽车品牌", dataIndex: "car_brand_disp_nm"},
        /*{ text: "型号", dataIndex: "car_type"},*/
        { text: "所属服务点", dataIndex: "svc_locate_disp_nm"},
        { text: "默认车位", dataIndex: "park_space"},
        { text: "上次洗车时间", dataIndex: "lcw_date", renderer: function(v) { return (!v) ? "无" : v; }},
        { text: "上次洗车地点", dataIndex: "lcw_addr", renderer: function(v) { return (!v) ? "无" : v; }, width: 300},
        { text: "注册时间", dataIndex: "reg_date"}
    ],
    autoScroll: true,
    bbar: Ext.create("Ext.PagingToolbar", {
        store: p00_cust.store,
        displayInfo: true,
        displayMsg: "显示客户 {0} - {1} / {2}",
        emptyMsg: "无记录"
    })
});


//show tab content
p00_cust.items = [ p00_cust.condPanel, p00_cust.gridPanel ];
crm.utils.showTabContent(p00_cust);

</script>

